package karileet.meeting.profile.fragment;

import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import karileet.meeting.profile.MeetingProfile;
import karileet.meeting.profile.ProfileConf;
import karileet.meeting.profile.R;
import karileet.meeting.profile.activity.ThirdPartyActivity;
import karileet.meeting.profile.activity.WeekdayActivity;
import karileet.meeting.profile.list.adapter.ListItemAdapter;
import karileet.meeting.profile.list.adapter.MergeAdapter;
import karileet.meeting.profile.list.adapter.ScheduleListItemAdapter;
import karileet.meeting.profile.list.holder.IMainListItemHolder;
import karileet.meeting.profile.list.item.IMainListItem;
import karileet.meeting.profile.list.item.ListItem;
import karileet.meeting.profile.list.item.ScheduleListItem;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class MainListFragment extends ListFragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        MergeAdapter complexList = new MergeAdapter();
        ArrayAdapter<ListItem> listAdapter = new ListItemAdapter(getActivity(),
                populateMainList());
        ArrayAdapter<ScheduleListItem> scheduleAdapter = new ScheduleListItemAdapter(
                getActivity(), populateScheduleList());
        complexList.addAdapter(listAdapter);
        complexList.addAdapter(scheduleAdapter);
        setListAdapter(complexList);
        ProfileConf pc = new ProfileConf(getActivity());
        if (!pc.isStopBegEnabled() && pc.setDailyReminder()) {
            FragmentManager manager = getFragmentManager();
            BeggarFragment begDialog = new BeggarFragment();
            begDialog.show(manager, "10");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        MeetingProfile mp = new MeetingProfile(getActivity());
        ProfileConf conf = new ProfileConf(getActivity());
        IMainListItemHolder holder = (IMainListItemHolder) v.getTag();
        IMainListItem item = holder.getTag();
        FragmentManager manager = getFragmentManager();
        item.toggle();
        switch (position) {
            case 0:
                if (item.isSelected()) {
                    Log.d(ProfileConf.APP, "Enabling profile");
                    mp.enable(true);
                } else {
                    Log.d(ProfileConf.APP, "Disabling profile");
                    mp.disable();
                }
                mp.updateWidget();
                break;
            case 1:
                Log.d(ProfileConf.APP, "Toggling vibrate: " + item.isSelected());
                ProfileConf pc = new ProfileConf(getActivity());
                pc.setVibrateEnabled(item.isSelected());
                mp.update();
                break;
            case 2:
                Log.d(ProfileConf.APP, "Showing volume dialog");
                VolumeFragment volumeDialog = new VolumeFragment();
                volumeDialog.show(manager, Integer.toString(position));
                break;
            case 3:
                // When accessiiblity is enabled, show app picker. Otherwise show info about how to enable it!
                if (conf.getAccessibilityEnabled()) {
                    Log.d(ProfileConf.APP, "Showing third party app integration");
                    Intent intent = new Intent(getActivity(), ThirdPartyActivity.class);
                    startActivity(intent);
                } else {
                    Log.d(ProfileConf.APP, "Showing enable accessibility info");
                    AccessibilityInfoFragment frag = new AccessibilityInfoFragment();
                    frag.show(manager, Integer.toString(position));
                }
                break;
            case 4:
                Log.d(ProfileConf.APP, "Opening market");
                Intent browserIntent = new Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse("https://play.google.com/store/apps/details?id=karileet.meeting.profile"));
                startActivity(browserIntent);
                break;
            case 5:
                Log.d(ProfileConf.APP, "Showing weekday picker");
                Intent i = new Intent(getActivity(), WeekdayActivity.class);
                startActivity(i);
                break;
            case 6:
                Log.d(ProfileConf.APP, "Showing schedule start dialog");
                StartDialog startDialog = new StartDialog();
                startDialog.show(manager, Integer.toString(position));
                break;
            case 7:
                Log.d(ProfileConf.APP, "Showing schedule stop dialog");
                StopDialog stopDialog = new StopDialog();
                stopDialog.show(manager, Integer.toString(position));
                break;
            default:
                break;
        }
        holder.setDescriptionText(item.getDescription());
        holder.setTitleText(item.getTitle());
    }

    private List<ListItem> populateMainList() {
        Resources res = getResources();
        String[] titles = res.getStringArray(R.array.listTitlesDefault);
        String[] titlesAlt = res.getStringArray(R.array.listTitlesActive);
        String[] descr = res.getStringArray(R.array.listDescriptionsDefault);
        String[] descrAlt = res.getStringArray(R.array.listDescriptionsActive);
        ProfileConf ps = new ProfileConf(getActivity().getApplicationContext());
        List<ListItem> list = new ArrayList<ListItem>();
        list.add(new ListItem(titles[0], descr[0], titlesAlt[0], descrAlt[0],
                ps.isProfileEnabled()));
        list.add(new ListItem(titles[1], descr[1], titlesAlt[1], descrAlt[1],
                ps.isVibrateEnabled()));
        list.add(new ListItem(titles[2], descr[2], titlesAlt[2], descrAlt[2],
                true));
        list.add(new ListItem(titles[5], descr[5], titlesAlt[5], descrAlt[5],
                true));
        list.add(new ListItem(titles[3], descr[3], titlesAlt[3], descrAlt[3],
                true));
        list.add(new ListItem(titles[4], descr[4], titlesAlt[4], descrAlt[4],
                true));
        return list;
    }

    private List<ScheduleListItem> populateScheduleList() {
        ProfileConf ps = new ProfileConf(getActivity());
        Resources res = getResources();
        Date start = new Date(ps.getScheduleStart());
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
        Date stop = new Date(ps.getScheduleStop());
        List<ScheduleListItem> list = new ArrayList<ScheduleListItem>();
        list.add(new ScheduleListItem(res.getString(R.string.enable), res
                .getString(R.string.schedule_start_descr), sdf.format(start),
                ps.isScheduleStartEnabled(), 0));
        list.add(new ScheduleListItem(res.getString(R.string.disable), res
                .getString(R.string.schedule_stop_descr), sdf.format(stop), ps
                .isScheduleStopEnabled(), 1));
        return list;
    }
}
